$(document).ready(function(){
	cw.common.getUserSession(validatePermission,true);
})

//验证权限
function validatePermission(userinfo){
	var permissionNames = "add,del,update";
	var actionNames = "ADD_T_SYS_ROLE_ACTION,DELETE_T_SYS_ROLE_ACTION,UPDATE_T_SYS_ROLE_ACTION";
	cw.common.hasPermission(permissionNames,actionNames,isShow);
	getRoleList();
}
//是否显示操作按钮
function isShow(data){
	var obj = data.root.PERMISSION.rs[0];
	if(obj.add){
		$('#add').html('<a href="addRole.html"><img src="../images/anniu16.gif"/></a>');
	}
	del = obj.del;
	if(del){
		$('#batchDel').html('<a href="javascript:batchDelRole()">批量删除</a>');
	}
	update = obj.update;
}

function getRoleList(){
	var url = cw.common.path + '/json?action=QUERY_T_SYS_ROLEBY_PAGER_ACTION';
	url += "&ROLE_NAME="+$('#ROLE_NAME').val();
	var xmlname = "T_SYS_ROLE";
	var pagePanel='#pager';
	var result='#rolelist';
	var pageSize=10;
	var defaultHtml='<li><em style="text-align: center;width:100%">没有搜索到您要查询的数据...</em></li>';
	cw.common.changePage(url,pageSize,xmlname,result,fn_getRoleList,pagePanel,defaultHtml);
}

function fn_getRoleList(obj,result,i){
	var str = "<li>";
	str += "<em style='width: 8%; text-align: center;'><input type='checkbox' name='check' value='"+obj.ROLE_ID+"' /></em>";
	str += "<em style='width: 20%; text-align: center;'>"+obj.ROLE_NAME+"</em>";
	var desc = obj.ROLE_DESC;
	if(desc.length == 0){
		str += "<em style='width: 51%; text-align: center;'>无</em>";
	}
	else{
		str += "<em style='width: 51%; text-align: center;'>"+obj.ROLE_DESC+"</em>";
	}
	str += "<em style='width: 20%; text-align: center;'>";
	if(obj.ROLE_ID != 1){
		if(update){
			str += "<a href='updateRole.html?id="+obj.ROLE_ID+"'>修改</a> ";
		}
		if(del){
			str += "<a href='javascript:delRole("+obj.ROLE_ID+",\""+obj.ROLE_NAME+"\")'>删除</a>";
		}
		if(!update && !del){
			str += "您无操作权限";
		}
	}
	else{
		str += "(不允许操作管理员角色)";
	}
	str += "</em>";
	$(result).append(str);
}

function delRole(roleid,rolename){
	if(confirm("是否确定删除："+rolename+"信息?")){
		var url = cw.common.path + '/json?action=DELETE_T_SYS_ROLE_ACTION&ROLE_ID='+roleid;
		$.post(url,{},function(data){
			if(data.success){
				alert('已成功删除系统角色：'+rolename);
				location.href = document.URL;
			} else {
				alert(data.errors.errmsg);
			}
		},'json');
	}
}

function batchDelRole(){
	if(getCheckedNum('check')==0){
		alert('请选择要删除的角色');
		return;
	}
	if(!confirm('确定要删除角色吗？')){
		return;
	}
	$.post(cw.common.path + '/json',
		{
			action : 'BATCH_DELETE_T_SYS_ROLE_ACTION',
			ROLE_ID : getCheckedVal('check').split(',')
		}, function(data){
			if (data.success) {
				alert('批量删除角色成功');
				location.href = document.URL;
			} else {
				alert(data.errors.errmsg);
			}
		}, 'json');
}
